﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Common.ErrorLogging;
using Sinacor.Infra.UI.Session;
using Sinacor.Infra.UI.Authorization;
using PSE.Framework.ErrorLogging;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;

namespace Sinacor.Infra.UI.ErrorLoggingEnvironment
{
    public class UserInterfaceEnvironmentCollector : SinacorEnvironmentCollector
    {
        protected override PSE.Framework.ErrorLogging.EnvironmentData CreateEnvironmentData()
        {
            return new UserInterfaceEnvironmentData();
        }

        protected override string GetVersion()
        {
            return FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location).FileVersion;
        }

        public override PSE.Framework.ErrorLogging.EnvironmentData Collect(Dictionary<string, object> extensionData)
        {
            EnvironmentData environmentData = base.Collect(extensionData) as EnvironmentData;

            if (environmentData != null &&
                environmentData is UserInterfaceEnvironmentData)
            {
                UserInterfaceEnvironmentData uiEnvironmentData = (UserInterfaceEnvironmentData)environmentData;

                try
                {
                    if (SessionManager.GetInstance() != null &&
                        SessionManager.GetInstance().GetCurrentSession() != null)
                    {
                        uiEnvironmentData.SinacorUserName = this.GetData<string>(() => SessionManager.GetInstance().GetCurrentSession().User.LoginName, "SinacorUserName", environmentData);
                        uiEnvironmentData.SinacorCompany = this.GetData<string>(() => SessionManager.GetInstance().GetCurrentSession().Company.Id + " - " + SessionManager.GetInstance().GetCurrentSession().Company.Name, "SinacorCompany", environmentData);
                    }

                    uiEnvironmentData.ScreensResolutions = this.GetData<List<string>>(GetScreensResolutions, "ScreensResolutions", environmentData);

                    if (extensionData != null)
                    {
                        if (extensionData.ContainsKey("SinacorModule"))
                        {
                            uiEnvironmentData.SinacorModule = this.GetData<string>(() => (string)extensionData["SinacorModule"], "SinacorModule", environmentData);
                        }

                        if (extensionData.ContainsKey("SinacorMenuPath"))
                        {
                            uiEnvironmentData.SinacorMenuPath = this.GetData<string>(() => (string)extensionData["SinacorMenuPath"], "SinacorMenuPath", environmentData);
                        }

                        if (extensionData.ContainsKey("SinacorWindowTitle"))
                        {
                            uiEnvironmentData.SinacorWindowTitle = this.GetData<string>(() => (string)extensionData["SinacorWindowTitle"], "SinacorWindowTitle", environmentData);
                        }
                    }
                }
                catch (Exception ex)
                {
                    uiEnvironmentData = (UserInterfaceEnvironmentData)HandleException(uiEnvironmentData, ex);
                }
            }

            return environmentData;
        }

        private List<string> GetScreensResolutions()
        {
            return Screen.AllScreens.ToList().ConvertAll<string>(s => string.Format("{0:n0}x{1:n0}", s.Bounds.Width, s.Bounds.Height));
        }
    }
}
